﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    ViewData["Title"] = "检测项设置";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">检测项设置</div>
        <div class="layui-card-body">
            <!-- 搜索条件 -->
            <div class="layui-form layui-form-pane" id="searchFormDiv">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">检测项编码</label>
                        <div class="layui-input-block">
                            <input type="text" name="searchCode" id="searchCode" autocomplete="off" placeholder="请输入检测项编码" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">检测项名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="searchName" id="searchName" autocomplete="off" placeholder="请输入检测项名称" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">检测项类型</label>
                        <div class="layui-input-block">
                            <input type="text" name="searchType" id="searchType" autocomplete="off" placeholder="请输入检测项类型" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button type="button" class="layui-btn layui-btn-normal" id="btnSearch">
                            <i class="layui-icon layui-icon-search"></i> 搜索
                        </button>
                        <button type="button" class="layui-btn layui-btn-primary" id="btnReset">
                            <i class="layui-icon layui-icon-refresh"></i> 重置
                        </button>
                    </div>
                </div>
            </div>

            <!-- 工具栏 -->
            <div class="layui-btn-container" style="margin-bottom: 10px;">
                <button type="button" class="layui-btn layui-btn-normal" id="btnAdd">
                    <i class="layui-icon layui-icon-add-1"></i> 新增
                </button>
                <button type="button" class="layui-btn layui-btn-danger" id="btnBatchDelete">
                    <i class="layui-icon layui-icon-delete"></i> 批量删除
                </button>
                <button type="button" class="layui-btn" id="btnExport">
                    <i class="layui-icon layui-icon-export"></i> 导出
                </button>
            </div>

            <!-- 数据表格 -->
            <table class="layui-hide" id="tableTestitemsettings" lay-filter="tableTestitemsettings"></table>

            <!-- 操作列模板 -->
            <script type="text/html" id="operationBar">
                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</a>
            </script>
        </div>
    </div>
</div>

<!-- 新增/编辑弹窗 -->
<div id="formDialog" style="display: none; padding: 30px; background-color: #fff; border-radius: 6px;">
    <form class="layui-form" id="formTestitemsetting" lay-filter="formTestitemsetting">
        <input type="hidden" name="Id" value="0" />
        
        <div class="layui-form-item">
            <div class="layui-inline" style="width: 48%;">
                <label class="layui-form-label" style="width: 110px;"><span style="color: #FF5722;">*</span> 检测项编码</label>
                <div class="layui-input-inline" style="width: 100%;">
                    <input type="text" name="ItemSettingsCode" lay-verify="required" autocomplete="off" placeholder="自动生成编码" class="layui-input" readonly>
                </div>
            </div>
            
            <div class="layui-inline" style="width: 48%;">
                <label class="layui-form-label" style="width: 110px;"><span style="color: #FF5722;">*</span> 检测项名称</label>
                <div class="layui-input-inline" style="width: calc(100% - 110px);">
                    <input type="text" name="ItemSettingsName" lay-verify="required" autocomplete="off" placeholder="请输入检测项名称" class="layui-input">
                </div>
            </div>
        </div>
        
        <div class="layui-form-item">
            <div class="layui-inline" style="width: 48%;">
                <label class="layui-form-label" style="width: 110px;"><span style="color: #FF5722;">*</span> 检测项类型</label>
                <div class="layui-input-inline" style="width: calc(100% - 110px);">
                    <select name="ItemSettingsType" lay-verify="required" lay-search>
                        <option value="">请选择检测项类型</option>
                        <option value="尺寸">尺寸</option>
                        <option value="外观">外观</option>
                        <option value="重量">重量</option>
                        <option value="性能">性能</option>
                        <option value="成分">成分</option>
                    </select>
                </div>
            </div>
            
            <div class="layui-inline" style="width: 48%;">
                <label class="layui-form-label" style="width: 110px;">检测工具</label>
                <div class="layui-input-inline" style="width: calc(100% - 110px);">
                    <input type="text" name="ItemSettingStool" autocomplete="off" placeholder="请输入检测工具" class="layui-input">
                </div>
            </div>
        </div>
        
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label" style="width: 110px;">备注</label>
            <div class="layui-input-block" style="margin-left: 110px;">
                <textarea name="Notes" placeholder="请输入内容" class="layui-textarea"></textarea>
            </div>
        </div>
        
        <div class="layui-form-item" style="text-align: center; margin-top: 30px;">
            <button type="button" class="layui-btn layui-btn-normal" lay-submit lay-filter="formSubmit" style="width: 120px;">确定</button>
            <button type="button" class="layui-btn layui-btn-primary" id="btnCancel" style="width: 120px; margin-left: 20px;">取消</button>
        </div>
    </form>
</div>

@section scripts {
    <script>
        layui.use(['table', 'form', 'layer', 'jquery'], function () {
            var table = layui.table,
                form = layui.form,
                layer = layui.layer,
                $ = layui.jquery;
                
            // 存储当前最大编码值
            var maxCode = 1;

            // 初始化表格
            var tableIns = table.render({
                elem: '#tableTestitemsettings',
                url: '/Testitemsettings/GetList',
                method: 'post',
                contentType: 'application/json',
                where: {},
                page: true,
                limits: [5, 10, 15, 20, 25, 30],
                limit: 5,
                even: true,
                cols: [[
                    { type: 'checkbox', fixed: 'left' },
                    { field: 'id', title: 'ID', width: 80, sort: true, fixed: 'left' },
                    { field: 'itemSettingsCode', title: '项目编码', width: 120 },
                    { field: 'itemSettingsName', title: '项目名称', width: 180 },
                    { field: 'itemSettingsType', title: '项目类型', width: 120 },
                    { field: 'itemSettingStool', title: '检测工具', width: 120 },
                    { title: '操作', width: 150, toolbar: '#operationBar', fixed: 'right' }
                ]],
                request: {
                    pageName: 'PageIndex',
                    limitName: 'PageSize'
                },
                parseData: function (res) {
                    if (res && res.data) {
                        // 查找当前最大编码值
                        res.data.forEach(function(item) {
                            if (item.itemSettingsCode && item.itemSettingsCode.startsWith('l')) {
                                var codeNum = parseInt(item.itemSettingsCode.substring(1));
                                if (!isNaN(codeNum) && codeNum >= maxCode) {
                                    maxCode = codeNum + 1;
                                }
                            }
                        });
                    }
                    return {
                        "code": res.code,
                        "msg": res.msg,
                        "count": res.count,
                        "data": res.data
                    };
                },
                response: {
                    statusCode: 0
                }
            });

            // 搜索按钮点击事件
            $('#btnSearch').click(function () {
                var searchParams = {
                    ItemSettingsCode: $('#searchCode').val(),
                    ItemSettingsName: $('#searchName').val(),
                    ItemSettingsType: $('#searchType').val()
                };
                
                tableIns.reload({
                    where: searchParams,
                    page: { curr: 1 }
                });
            });

            // 重置按钮点击事件
            $('#btnReset').click(function () {
                // 清空搜索条件
                $('#searchCode').val('');
                $('#searchName').val('');
                $('#searchType').val('');
                
                // 重新加载表格
                tableIns.reload({
                    where: {
                        ItemSettingsCode: '',
                        ItemSettingsName: '',
                        ItemSettingsType: ''
                    },
                    page: { curr: 1 }
                });
            });

            // 导出按钮点击事件
            $('#btnExport').click(function () {
                window.location.href = '/Testitemsettings/Export';
            });

            // 新增按钮点击事件
            $('#btnAdd').click(function () {
                // 重置表单
                document.getElementById("formTestitemsetting").reset();
                $('input[name="Id"]').val(0);
                
                // 自动生成编码
                $('input[name="ItemSettingsCode"]').val('l' + maxCode.toString().padStart(4, '0'));
                
                // 打开弹窗
                layer.open({
                    type: 1,
                    title: '添加检测项',
                    area: ['700px', '400px'],
                    content: $('#formDialog'),
                    skin: 'layui-layer-molv', // 美化弹窗
                    success: function () {
                        form.render();
                    }
                });
            });

            // 取消按钮点击事件
            $('#btnCancel').click(function(){
                layer.closeAll('page');
            });

            // 批量删除按钮点击事件
            $('#btnBatchDelete').click(function () {
                var checkStatus = table.checkStatus('tableTestitemsettings');
                var data = checkStatus.data;
                
                if (data.length === 0) {
                    layer.msg('请选择要删除的数据', { icon: 2 });
                    return;
                }

                layer.confirm('确定要删除选中的' + data.length + '条数据吗？', { icon: 3, title: '提示' }, function (index) {
                    var ids = data.map(item => item.id);
                    
                    // 调用批量删除接口
                    $.ajax({
                        url: '/Testitemsettings/BatchDelete',
                        type: 'POST',
                        contentType: 'application/json',
                        data: JSON.stringify(ids),
                        success: function (res) {
                            if (res.success) {
                                layer.msg('删除成功', { icon: 1 });
                                tableIns.reload();
                            } else {
                                layer.msg(res.message || '删除失败', { icon: 2 });
                            }
                        },
                        error: function () {
                            layer.msg('服务器错误', { icon: 2 });
                        }
                    });
                    layer.close(index);
                });
            });

            // 表单提交事件
            form.on('submit(formSubmit)', function (data) {
                var formData = data.field;
                var id = parseInt(formData.Id);
                
                // 如果是新增，递增编码
                if (id === 0) {
                    maxCode++;
                }
                
                var url = id > 0 ? '/Testitemsettings/Update?id=' + id : '/Testitemsettings/Create';
                
                $.ajax({
                    url: url,
                    type: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify(formData),
                    success: function (res) {
                        if (res.success) {
                            layer.closeAll('page');
                            layer.msg(id > 0 ? '修改成功' : '添加成功', { icon: 1 });
                            tableIns.reload();
                        } else {
                            layer.msg(res.message || (id > 0 ? '修改失败' : '添加失败'), { icon: 2 });
                        }
                    },
                    error: function () {
                        layer.msg('服务器错误', { icon: 2 });
                    }
                });
                
                return false;
            });

            // 表格工具条事件
            table.on('tool(tableTestitemsettings)', function (obj) {
                var data = obj.data;
                var event = obj.event;
                
                if (event === 'edit') {
                    // 重置表单并填充数据
                    document.getElementById("formTestitemsetting").reset();
                    
                    form.val('formTestitemsetting', {
                        "Id": data.id,
                        "ItemSettingsCode": data.itemSettingsCode,
                        "ItemSettingsName": data.itemSettingsName,
                        "ItemSettingsType": data.itemSettingsType,
                        "ItemSettingStool": data.itemSettingStool,
                        "Notes": data.notes
                    });
                    
                    // 编辑时设置编码为只读状态
                    $('#formDialog input[name="ItemSettingsCode"]').attr('readonly', true);
                    
                    // 打开弹窗
                    layer.open({
                        type: 1,
                        title: '编辑检测项',
                        area: ['700px', '400px'],
                        content: $('#formDialog'),
                        skin: 'layui-layer-molv', // 美化弹窗
                        success: function () {
                            form.render();
                        }
                    });
                } else if (event === 'delete') {
                    layer.confirm('确定要删除该条数据吗？', { icon: 3, title: '提示' }, function (index) {
                        // 调用删除接口
                        $.ajax({
                            url: '/Testitemsettings/Delete?id=' + data.id,
                            type: 'POST',
                            success: function (res) {
                                if (res.success) {
                                    obj.del();
                                    layer.msg('删除成功', { icon: 1 });
                                } else {
                                    layer.msg(res.message || '删除失败', { icon: 2 });
                                }
                            },
                            error: function () {
                                layer.msg('服务器错误', { icon: 2 });
                            }
                        });
                        layer.close(index);
                    });
                }
            });
        });
    </script>
}

